package com.cl.ks.service;


import com.cl.ks.entity.KsFlowGraph;
import com.cl.ks.entity.KsFlowGraphRecord;
import com.cl.ks.repository.KsFlowGraphRecordRepository;
import com.cl.ks.utils.DbUtil;
import com.cl.mvc.service.BaseService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.cl.ks.constant.QTableConstant.qKsFlowGraphRecord;

@Service
public class KsFlowGraphRecordService extends BaseService<KsFlowGraphRecord> {
    private final JPAQueryFactory queryFactory;
    private final DbUtil dbUtil;

    protected KsFlowGraphRecordService(KsFlowGraphRecordRepository KsFlowGraphRecordRepository, JPAQueryFactory queryFactory, DbUtil dbUtil) {
        super(KsFlowGraphRecordRepository);
        this.queryFactory = queryFactory;
        this.dbUtil = dbUtil;
    }

    public Page<KsFlowGraphRecord> pageLastRecord(KsFlowGraphRecord entity, String auth) {
        List<Map<String, Object>> query = dbUtil.fetchSQL(null,"SELECT id\n" +
                "FROM ks_flow_graph_record r\n" +
                "         INNER JOIN (\n" +
                "    SELECT ks_flow_graph_id, MAX(create_time) AS max_create_time\n" +
                "    FROM ks_flow_graph_record\n" +
                "    GROUP BY ks_flow_graph_id\n" +
                ") AS latest_records\n" +
                "                    ON r.ks_flow_graph_id = latest_records.ks_flow_graph_id AND r.create_time = latest_records.max_create_time;");
        List<Integer> ids = query.stream().map(item->(Integer)item.get("id")).collect(Collectors.toList());
        entity.getParams().put("idIn", StringUtils.join(ids,","));

        return repository.findAll(buildSpecification(entity).and(buildPowerSpecification(auth)), PageRequest.of(entity.getPageNumber() - 1, entity.getPageSize()));
    }

    public KsFlowGraphRecord saveKsFlowGraphRecord(KsFlowGraph ksFlowGraph) {
        KsFlowGraphRecord ksFlowGraphRecord = new KsFlowGraphRecord();
        ksFlowGraphRecord.setKsFlowGraphId(ksFlowGraph.getId());
        ksFlowGraphRecord.setKsFlowTaskId(ksFlowGraph.getKsFlowTaskId());
        ksFlowGraphRecord.setCreatedBy(ksFlowGraph.getCreatedBy());
        ksFlowGraphRecord.setCreatedByShowName(ksFlowGraph.getCreatedByShowName());
        ksFlowGraphRecord.setTaskFlag(ksFlowGraph.getTaskFlag());
        ksFlowGraphRecord.setStatus(0);
        save(ksFlowGraphRecord);
        List<Integer> oldRecordIds = queryFactory.select(qKsFlowGraphRecord.id).from(qKsFlowGraphRecord).where(qKsFlowGraphRecord.ksFlowGraphId.eq(ksFlowGraph.getId())).orderBy(qKsFlowGraphRecord.id.desc()).offset(365).fetch();
        if (!oldRecordIds.isEmpty()) {
            deleteAllById(oldRecordIds);
        }
        return ksFlowGraphRecord;
    }
}
